package cn.com.cym.gamezao.util;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * 设置非对称加密
 */
public class RSAUtils {



    public static String PUBLIC_KEY;

    private static String PRIVATE_KEY;

    static {
        KeyPairGenerator keyGen = null;
        try {
            keyGen = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        //断言 keyGen不为空
        assert keyGen != null;
        keyGen.initialize(512);
        KeyPair pair = keyGen.generateKeyPair();

        PUBLIC_KEY= base64Encode(pair.getPublic().getEncoded());
        PRIVATE_KEY = base64Encode(pair.getPrivate().getEncoded());

    }


    /**
     * base64
     * @param bytes b
     * @return string
     */
    static String base64Encode(byte[] bytes) {

        return Base64.getEncoder().encodeToString(bytes);
    }

    /**
     *
     * @param str s
     * @return byte[]
     */
    static byte[] base64Decode(String str) {

        return Base64.getDecoder().decode(str);
    }

    /**
     * 加密
     * @param data data
     * @param publicKey publicKey
     * @return String
     * @throws GeneralSecurityException
     */
    public static String encrypt(String data, String publicKey)  {

        EncodedKeySpec keySpec = new X509EncodedKeySpec(base64Decode(publicKey));
        String s="";
        try{
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");

            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(keySpec));
            s= base64Encode(cipher.doFinal(data.getBytes()));
        }catch (GeneralSecurityException e){
            e.printStackTrace();
        }
        return s;
    }


    /**
     * 解密
     * @param data data
     * @param privateKey privateKey
     * @return String
     * @throws GeneralSecurityException
     */
    public static String decrypt(String data, String privateKey) throws GeneralSecurityException {

        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(base64Decode(privateKey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, keyFactory.generatePrivate(keySpec));
        return new String(cipher.doFinal(base64Decode(data)));
    }

/*    public static void main(String[] args) throws GeneralSecurityException {

        System.out.println(PUBLIC_KEY);

        System.out.println(PRIVATE_KEY);

        String encrypt = encrypt("1234567", PUBLIC_KEY);

        System.out.println("加密:"+encrypt);

        String decrypt = decrypt(encrypt, PRIVATE_KEY);

        System.out.println("解密:"+decrypt);


    }*/

     /*MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMmKRWOnXXPBbtRp2huFlWat4Br7MfeTqO1OFZtTaikpwmUeQUEkvIq7ToEusDS/FzFBveqsSu07MVdPPGt6PhMCAwEAAQ==
           加密:mb2EeJMbQikQYdfqAAaQHaHVuhjQ8JxYpLt8YmYfY9+in1gvAbazFdSej/Phq+hvU82zbOlRv4ef77V0+6VZZQ==
           解密：1234567*/
}
